package com.hyt.it.ogt.download.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.it.ogt.download.entity.Student;
import com.hyt.it.ogt.download.entity.Video;
import com.hyt.it.ogt.download.enums.SearchType;
import com.hyt.it.ogt.download.mapper.VideoMapper;
import com.hyt.it.ogt.download.service.IVideoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class VideoServiceImpl implements IVideoService {
    @Resource
    private VideoMapper videoMapper;

    @Override
    public IPage<Student> getVideoList(String officeId, String examCode, String searchText, String searchType,
                                       Integer current, Integer pageSize) {
        IPage<Student> page = new Page();
        page.setCurrent(current);
        page.setSize(pageSize);
        String examId = this.getExamId(examCode, officeId);
        if(StringUtils.isBlank(examId)) {
            return page;
        }
        if (StringUtils.isNotBlank(examId)) {
            searchText = "%" + searchText + "%";
            String nameId = videoMapper.getNameConfig(examId);
            if (SearchType.SEARCH_BY_NAME.getCode().equalsIgnoreCase(searchType)) {
                page = videoMapper.getVideoPageByName(page, examId, nameId, searchText);
            } else if (SearchType.SEARCH_BY_ADMISSION_NUMBER.getCode().equalsIgnoreCase(searchType)) {
                page = videoMapper.getVideoPageByAdmissionNumber(page, examId, searchText);
            } else {
                page = videoMapper.getVideoPage(page, examId);
            }
            this.getStudentVideo(page, nameId, examId);
        }
        return page;
    }

    private void getStudentVideo(IPage<Student> page, String nameId, String examId) {
        for (Student student : page.getRecords()) {
            String id = student.getId();
            student.setStudentName(videoMapper.getCandidateName(examId, id, nameId));
            List<Video> videoList = videoMapper.getVideoList(examId, id);
            List<Video> pcVideoList = new ArrayList<>();
            List<Video> mobileVideoList = new ArrayList<>();
            if (videoList.size() > 0) {
                videoList.stream().forEach(item -> {
                    if (StringUtils.isNotBlank(item.getFileUrl())) {
                        switch (item.getRecordDevice()) {
                            case 0:
                                pcVideoList.add(item);
                                break;
                            case 1:
                                mobileVideoList.add(item);
                                break;
                        }
                    }
                });
            }
            student.setMobileVideoList(mobileVideoList);
            student.setPcVideoList(pcVideoList);
        }
    }

    private String getExamId(String examCode, String officeId) {
        return videoMapper.getExamId(examCode, officeId);
    }
}
